package cn.bdqn.pro.service.impl;

import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.pro.mapper.TransMapper;
import cn.bdqn.pro.mapper.TransportMapper;
import cn.bdqn.pro.model.Track;
import cn.bdqn.pro.model.Trans;
import cn.bdqn.pro.model.TransExample;
import cn.bdqn.pro.model.Transport;
import cn.bdqn.pro.service.TransportService;
import cn.bdqn.pro.vomapper.TrackVoMapper;
import cn.bdqn.service.BaseService;
import cn.bdqn.veh.model.Driver;
import cn.bdqn.veh.model.Leader;
import cn.bdqn.veh.vomapper.DriverVoMapper;
import cn.bdqn.veh.vomapper.VehicleVoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/**
 * @author gurilla
 * @create 2019-09-10 11:37
 */
@Service
public class TransportServiceImpl extends BaseService implements TransportService {

    @Autowired
    private TransportMapper transportMapper;

    @Autowired
    private TransMapper transMapper;

    @Autowired
    private VehicleVoMapper vehicleVoMapper;

    @Autowired
    private DriverVoMapper driverVoMapper;

    @Autowired
    private TrackVoMapper trackVoMapper;

    @Override
    public EUList<Trans> list(PageBean pageBean, String lname, String cname, String vidcard) {
        TransExample example = new TransExample();
        TransExample.Criteria criteria = example.createCriteria();
        //       criteria.andEidEqualTo(getEid());   //从session来
        if(!StringUtils.isEmpty(lname) || !StringUtils.isEmpty(cname) || !StringUtils.isEmpty(vidcard)){
            criteria.andCnameLike("%"+cname+"%");
            criteria.andLnameLike("%"+lname+"%");
            criteria.andVidcardLike("%"+vidcard+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<Trans> trans = transMapper.selectByExample(example);
        PageInfo<Trans> info=new PageInfo<>(trans);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Transport transport) {
        if(transport.getId()==null){
            transport.setPaymentStatus(false);
            transport.setInvoiceStatus(false);
            transport.setOpid(getUserid());
            transportMapper.insertSelective(transport);
        }else{
            transport.setOpid(getUserid());
            transportMapper.updateByPrimaryKeySelective(transport);
        }
        return R.ok();
    }

    @Override
    public R getLeader() {
        List<Leader> leaders = vehicleVoMapper.getAllLeader();
        return R.ok(null, leaders);
    }

    @Override
    public R getDriver() {
        List<Driver> drivers = driverVoMapper.getAllDriver();
        return R.ok(null, drivers);
    }

    @Override
    public R getTrack() {
        List<Track> tracks = trackVoMapper.getAllTrack();
        return R.ok(null, tracks);
    }
}
